﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace CitizenDiary.Sync.Contract
{
    [DataContract]
    public class HandshakeRequest
    {
        [DataMember]
        public Guid DeviceID { get; set; }

        [DataMember]
        public string LastSyncOn { get; set; }

        [DataMember]
        public IDictionary<Guid, DeviceDiaryItem> DeviceItems { get; set; }

        public HandshakeRequest()
        {
            DeviceItems = new Dictionary<Guid, DeviceDiaryItem>();
        }

        public DateTime GetLastSyncOnDateTime()
        {
            DateTime lastSyncOn;
            DateTime.TryParse(LastSyncOn, out lastSyncOn);
            return lastSyncOn;
        }

        [DataContract]
        public class DeviceDiaryItem
        {
            [DataMember]
            public long UpdatedVersion { get; set; }

            [DataMember]
            public Guid UpdatedDeviceID { get; set; }

            [DataMember]
            public long BasedVersion { get; set; }

            [DataMember]
            public Guid BasedDeviceID { get; set; }

            public bool HasChanged()
            {
                return UpdatedDeviceID != BasedDeviceID || UpdatedVersion != BasedVersion;
            }
        }
    }
}
